package com.molyfun.parents.activity.course.view.courseitem;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.ImageViewerActivity;
import com.molyfun.parents.activity.course.bean.CourseMediaBean;
import com.molyfun.parents.dao.entity.SingleDownloadTask;
import com.molyfun.parents.dao.gen.SingleDownloadTaskDao;
import com.molyfun.parents.rest.QiniuSDK;
import com.molyfun.parents.utils.Const;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class CourseImageView extends BaseCourseView implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.course_cell_image_img)
    ImageView mCourseCellImg;

    private SingleDownloadTask mSingleDownloadTask;

    public CourseImageView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public CourseImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CourseImageView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.layout_course_cell_image, this);
        ButterKnife.inject(this);

    }

    /**
     * init view
     *
     */
    public void initData(CourseMediaBean courseMediaBean) {
        super.initData(courseMediaBean);
        if(courseMediaBean == null){
            return;
        }
        if(!TextUtils.isEmpty(courseMediaBean.getUrl())){
            if(mCourseMediaBean.isOffLineMode()){
                //TODO 本地加载图片

                List<SingleDownloadTask> singleDownloadTasks = singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.Url.eq(courseMediaBean.getUrl())).build().list();
                if(singleDownloadTasks != null && singleDownloadTasks.size() > 0){
                    mSingleDownloadTask = singleDownloadTasks.get(0);
                }
                if(mSingleDownloadTask != null) {
                    if(!TextUtils.isEmpty(mSingleDownloadTask.getPath())){
                        mCourseCellImg.setImageURI(Uri.fromFile(new File(mSingleDownloadTask.getPath())));
                    }else{
                        if(mSingleDownloadTask.getState() == Const.DOWNLOAD_STATE_FAILED){
                            mCourseCellImg.setImageResource(R.mipmap.ic_pic_failed_holder);
                        }else{
                            mCourseCellImg.setImageResource(R.mipmap.ic_pic_holder);
                        }
                    }
                }
            }else{
                Picasso.with(context)
                        .load(QiniuSDK.getSmallUrl(courseMediaBean.getUrl()))
                        .placeholder(R.mipmap.ic_pic_holder)
                        .into(mCourseCellImg);
            }

        }else{
            mCourseCellImg.setImageResource(R.mipmap.ic_pic_holder);
        }
        mCourseCellImg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.course_cell_image_img:
                if(mCourseMediaBean.isOffLineMode()) {
                    //TODO 本地加载图片
                    if(mSingleDownloadTask != null){
                        ImageViewerActivity.startActivity(context, "file://" + mSingleDownloadTask.getPath());
                    }
                }else{
                    ImageViewerActivity.startActivity(context, QiniuSDK.getBigUrl(mCourseMediaBean.getUrl()));
                }
                break;

        }
    }

}